import {
	mapState
} from "vuex";
import {
} from '@/common/api.js'
import {
	baseUrl
} from '@/common/config.js'
export default {
	data() {},
	computed: {
		...mapState(["$userInfo","$companyInfo","isLogin","token"]),
	},
	methods: {
		jumpPage(url, type) {
			if (this.isLogin) {
				uni.navigateTo({
					url
				})
			} else {
				uni.showModal({
					title:"温馨提示",
					content:"请登陆账号",
					showCancel:false,
					success() {
						uni.redirectTo({
							url:'/pages/common/login'
						})
					}
				})
			}
		},
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			uni.showLoading({
				title:'上传中'
			})
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				}))
				fileListLen++
			}
		
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					url: baseUrl + '/api/common/upload', // 仅为示例，非真实的接口地址
					filePath: url,
					name: 'file',
					formData: {
						token: this.token
					},
					success: (res) => {
						let data = JSON.parse(res.data);
							uni.hideLoading();
							
							console.log('res', data.data.fullurl);
							resolve(data.data.fullurl)
						// setTimeout(() => {
						
						// }, 1000)
					}
				});
			})
		},
		hideKeyboard() {
			uni.hideKeyboard()
		}
	}
}